//
//  LGRulerView.m
//  SportRing
//
//  Created by mykoma on 14-4-7.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "LGRulerView.h"

#define DEFAULT_START_OFFSET (0)
#define DEFAULT_END_OFFSET (100)
#define DEFAULT_EACH_OFFSET (10)

@interface LGRulerView () <UIScrollViewDelegate>

@property (nonatomic, assign) NSInteger idxOffset;

@end

@implementation LGRulerView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.delegate = self;
        self.eachOffset = 1;
        self.direction = LG_DIRECTION_TYPE_HORIZONTAL;
    }
    return self;
}

-(CGFloat)eachDistance
{
    return DEFAULT_EACH_OFFSET;
}

-(void)drawRulerVertical
{
    float counter = self.beginNum;
    while (counter <= self.endNum)
    {
        int idx = (counter - self.beginNum) / self.eachOffset;
        // 添加竖线
        UIView * tempView = [[UIView alloc] init];
        
        // 当计数是5的倍数的时候
        if (idx % 5 == 0)
        {
            tempView.frame = CGRectMake(50,
                                        idx * DEFAULT_EACH_OFFSET, 25, 2);
            tempView.backgroundColor = [UIColor darkGrayColor];
        }
        // 当计数不是5的倍数的时候
        else
        {
            tempView.frame = CGRectMake(60,
                                        idx * DEFAULT_EACH_OFFSET, 15, 2);
            tempView.backgroundColor = [UIColor lightGrayColor];
        }
        
        // 如果当前计数idx和要选中的是相等, 那么设置颜色为红色
        if (idx == self.idxOffset)
        {
            tempView.backgroundColor = [UIColor redColor];
        }
        
        [self addSubview:tempView];
        
        // 每十个显示一次数值
        if (idx % 10 == 0)
        {
            UILabel * numLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,
                                                                           idx * DEFAULT_EACH_OFFSET - 10, 40, 30)];
            numLabel.text = [@(counter) stringValue];
            [self addSubview:numLabel];
        }
        
        // 每次循环完成自增
        counter += self.eachOffset;
    }
}

-(void)drawRulerHorizontal
{
    float counter = self.beginNum;
    while (counter <= self.endNum)
    {
        int idx = (counter - self.beginNum) / self.eachOffset;
        // 添加竖线
        UIView * tempView = [[UIView alloc] init];
        
        // 当计数是5的倍数的时候
        if (idx % 5 == 0)
        {
            tempView.frame = CGRectMake(idx * DEFAULT_EACH_OFFSET,
                                        10, 2, 15);
            tempView.backgroundColor = [UIColor darkGrayColor];
        }
        // 当计数不是5的倍数的时候
        else
        {
            tempView.frame = CGRectMake(idx * DEFAULT_EACH_OFFSET,
                                        10, 2, 10);
            tempView.backgroundColor = [UIColor lightGrayColor];
        }
        
        // 如果当前计数idx和要选中的是相等, 那么设置颜色为红色
        if (idx == self.idxOffset)
        {
            tempView.backgroundColor = [UIColor redColor];
        }
        
        [self addSubview:tempView];
        
        // 每十个显示一次数值
        if (idx % 10 == 0)
        {
            UILabel * numLabel = [[UILabel alloc] initWithFrame:CGRectMake(idx * DEFAULT_EACH_OFFSET - 10,
                                                                           30, 40, 30)];
            numLabel.text = [@(counter) stringValue];
            [self addSubview:numLabel];
        }
        
        // 每次循环完成自增
        counter += self.eachOffset;
    }
}

-(void)layoutSubviews
{
    [self.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [obj removeFromSuperview];
    }];
    if (self.direction == LG_DIRECTION_TYPE_HORIZONTAL)
    {
        [self drawRulerHorizontal];
    }
    else
    {
        [self drawRulerVertical];
    }
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetX = (self.direction == LG_DIRECTION_TYPE_HORIZONTAL) ? scrollView.contentOffset.x : scrollView.contentOffset.y;
    self.idxOffset = offsetX / DEFAULT_EACH_OFFSET + self.selectOffset;
    
    if ([self.rulerDelegate respondsToSelector:@selector(selectNumber:)])
    {
        NSNumber * number = [NSNumber numberWithFloat:self.beginNum + self.idxOffset];
        [self.rulerDelegate selectNumber:number];
    }
}

@end